Adds TypeAlias annotation, where possible#11321
Adds TypeAlias annotation, where possible#11321hauntsaninja merged 4 commits intopython:masterfrom sobolevn:explicit-typealiases
TypeAlias annotation, where possible#11321Conversation
|
Looks like it does not work out of the box. Some more work is required. |
|
Thanks for doing this, will take a look later. I suspect at least some of the confusion might just be from clobbering mypy.nodes.TypeAlias Lines 2877 to 2878 in c22beb4 |
mypy/checker.py
Outdated
| Iterable, Sequence, Mapping, Generic, AbstractSet, Callable | ||
| ) | ||
| from typing_extensions import Final | ||
| from typing_extensions import Final, TypeAlias |
There was a problem hiding this comment.
This file also imports mypy.nodes.TypeAlias (line 25)
mypy/checkexpr.py
Outdated
| Any, cast, Dict, Set, List, Tuple, Callable, Union, Optional, Sequence, Iterator | ||
| ) | ||
| from typing_extensions import ClassVar, Final, overload | ||
| from typing_extensions import ClassVar, Final, overload, TypeAlias |
There was a problem hiding this comment.
Thanks! Now TypeAlias from typing_extensions is always called _TypeAlias
|
@hauntsaninja is right. I wonder why flake8 doesn't catch this. |
|
Looks like the CI is stuck. Reopening to retrigger it. |
hauntsaninja
left a comment
There was a problem hiding this comment.
Hooray, looks like it's passing tests.
I'd recommend using _TypeAlias in every file, not just ones that we have mypy.nodes.TypeAlias in. Actually, thinking about it, I think the clearest thing to do is just use the fully qualified typing_extensions.TypeAlias everywhere in mypy. There aren't too many of these, so the extra verbosity is probably worth it.
I think we'll also need to change the minimum version of typing_extensions we require.
I've already done this 🎉
I cannot agree with this, sorry! This is way too verbose. Imagine this: import typing_extensions
TypeParameterChecker: typing_extensions.TypeAlias = Callable[[Type, Type, int], bool]Current solution: from typing_extensions import TypeAlias as _TypeAlias
TypeParameterChecker: _TypeAlias = Callable[[Type, Type, int], bool]Looks and reads much better! 😎 |
mypy/report.py
Outdated
| ) | ||
|
|
||
| ReporterClasses = Dict[str, Tuple[Callable[['Reports', str], 'AbstractReporter'], bool]] | ||
| ReporterClasses: TypeAlias = Dict[str, Tuple[Callable[['Reports', str], 'AbstractReporter'], bool]] |
There was a problem hiding this comment.
| ReporterClasses: TypeAlias = Dict[str, Tuple[Callable[['Reports', str], 'AbstractReporter'], bool]] | |
| ReporterClasses: _TypeAlias = Dict[str, Tuple[Callable[['Reports', str], 'AbstractReporter'], bool]] |
mypy/scope.py
Outdated
|
|
||
|
|
||
| SavedScope = Tuple[str, Optional[TypeInfo], Optional[FuncBase]] | ||
| SavedScope: TypeAlias = Tuple[str, Optional[TypeInfo], Optional[FuncBase]] |
There was a problem hiding this comment.
| SavedScope: TypeAlias = Tuple[str, Optional[TypeInfo], Optional[FuncBase]] | |
| SavedScope: _TypeAlias = Tuple[str, Optional[TypeInfo], Optional[FuncBase]] |
mypy/typestate.py
Outdated
|
|
||
| # Represents that the 'left' instance is a subtype of the 'right' instance | ||
| SubtypeRelationship = Tuple[Instance, Instance] | ||
| SubtypeRelationship: TypeAlias = Tuple[Instance, Instance] |
There was a problem hiding this comment.
| SubtypeRelationship: TypeAlias = Tuple[Instance, Instance] | |
| SubtypeRelationship: _TypeAlias = Tuple[Instance, Instance] |
mypy/typestate.py
Outdated
| # A tuple encoding the specific conditions under which we performed the subtype check. | ||
| # (e.g. did we want a proper subtype? A regular subtype while ignoring variance?) | ||
| SubtypeKind = Tuple[bool, ...] | ||
| SubtypeKind: TypeAlias = Tuple[bool, ...] |
There was a problem hiding this comment.
| SubtypeKind: TypeAlias = Tuple[bool, ...] | |
| SubtypeKind: _TypeAlias = Tuple[bool, ...] |
mypy/typestate.py
Outdated
| # A cache that keeps track of whether the given TypeInfo is a part of a particular | ||
| # subtype relationship | ||
| SubtypeCache = Dict[TypeInfo, Dict[SubtypeKind, Set[SubtypeRelationship]]] | ||
| SubtypeCache: TypeAlias = Dict[TypeInfo, Dict[SubtypeKind, Set[SubtypeRelationship]]] |
There was a problem hiding this comment.
| SubtypeCache: TypeAlias = Dict[TypeInfo, Dict[SubtypeKind, Set[SubtypeRelationship]]] | |
| SubtypeCache: _TypeAlias = Dict[TypeInfo, Dict[SubtypeKind, Set[SubtypeRelationship]]] |
|
What if we rename |
|
We already have both a TypeAlias and a TypeAliasType + might be a breaking change for plugins. I'm open to it, but I'm struggling to come up with names that work. Maybe we should just rewrite all of mypy in TypeScript? ;-) |
|
Thanks! I've missed a couple. Now it should be ready!
I am planning to do it in Rust 😉 |
|
Thank you! |
This is more an experiment to see if newly added
TypeAliasin our own code is supported.Refs #11305